﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FundamentalsOfCompiling{  
    class Facade
    {
        private WordParser wordparser;
        private Syntax syntax;
        private Global gl;
        private List<TokenType> list;
        private ISource source;
        public Facade() { gl = Global.createGlobal();}
        public void Init(ISource s)
        {
            gl.init();
            list = new List<TokenType>();
            this.source = s;
            wordparser = new WordParser(this.gl,this.source,this.list);
            wordparser.getTokenList();
            syntax=new Syntax(gl,list);
        }
        public List<TokenType> getTokenList() { 
            if (list != null)
                return list;
            else throw new ParserException("词法分析有异常");
           
        } 
        public void  SyntaxParse(){
            if(syntax!=null)
           syntax.parse();
        }
        public void printTree(Midiator midiator) {
            syntax.printTree(midiator);
        }
        public ISource createSource(sourceType type,String str){
            if (str == null) throw new ParserException("要分析的文件或字符串为空");
            if (type == sourceType.STRING) return new SourceString(str);
            else return new SourceFile(str);
        }
        public void printTokenList(Midiator midiator) {
            wordparser.printTokenList(midiator);
        }   
    }
}
